﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;
using Worktool.CODE_Chris.DataSourceHelpers;
using Worktool.CODE_Chris.SQL_DATA;

namespace Worktool
{
    public partial class ReportRacks : Form
    {
        private SQL sql;
        private ReportRacksHelper helper;
        private Boolean isInit = true;

        public ReportRacks()
        {
            InitializeComponent();
            this.Text = "Racks: " + Login.currentDBName;
            init();
        }

        private void init()
        {
            sql = new SQL();
            helper = new ReportRacksHelper(reportViewer1, this);

            fillElementsInit();

            fillTablesInit();

            isInit = false;
        }

        #region Init
        private void fillTablesInit()
        {
            try
            {
                helper.getData1();
            }
            catch (Exception e)
            {

            }
        }

        private void fillElementsInit()
        {
            try
            {
                String[,] rack = sql.receiveSQL("SELECT COUNT(*) AS Amount FROM vw_rep_Racklist", "SELECT Remarks AS ID, Venue + ' ' + Remarks AS Kombi FROM vw_rep_Racklist ORDER BY Venue");

                //Area Combobox füllen
                fillComboBox(comboBox1, rack);
            }
            catch (Exception e)
            {

            }
        }

        private void fillComboBox(ComboBox box, String[,] data)
        {
            try
            {
                box.DropDownStyle = ComboBoxStyle.DropDownList;
                box.Items.Clear();
                box.Items.Add("");
                box.DropDownWidth = 300;
                box.DropDownHeight = 500;
                for (int a = 0; a < data.GetLength(1); a++)
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = data[0, a];
                    item.Text = data[1, a];
                    box.Items.Add(item);
                }

                box.Refresh();
                box.Text = data[1, 0];
            }
            catch (Exception e)
            {
                sql.log("FEHLER", e, this.ToString() + ":" + System.Reflection.MethodBase.GetCurrentMethod().Name);
            }
        }

        public class ComboboxItem
        {
            public string Text { get; set; }
            public object Value { get; set; }

            public override string ToString()
            {
                return Text;
            }
        }
        #endregion

        #region Filter
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!isInit)
            {
                try
                {
                    ReportParameter[] parameters = new ReportParameter[1];
                    parameters[0] = new ReportParameter("uberschrift", comboBox1.Text);
                    
                    reportViewer1.LocalReport.SetParameters(parameters);
                    if (helper.getTable1() != null) helper.getTable1().DefaultView.RowFilter = string.Format("[Rack Nr.] LIKE '{0}'", (comboBox1.SelectedItem as ComboboxItem).Value.ToString() + "*");
                    reportViewer1.RefreshReport();
                }
                catch (Exception ex)
                {

                }
            }
        }
        #endregion


    }
}
